class AdController < ApplicationController
  #权限验证
  before_filter :auth
  layout "admin"
  def index
    redirect_to :action=>'list'
  end

  def new
    @ad = Ad.new
    @titles = "新增广告"
  end


  def create
    @ad = Ad.new(params[:ad])
    if  @ad && @ad.save     
      flash[:notice] = '搞定一个！'
      redirect_to :action=>'list'
    else
      render :action => "new"
    end
  end


  #查询
  def list
    #提交表单为post
    if request.post?
      @ad_search = params[:ad]
      name = @ad_search[:name]
      @ads = Ad.paginate :per_page => $perPage, :page =>  params[:page],
        :conditions => ['name like ?', "%#{name}%"], :order => 'orders asc'
    else
      @ads = Ad.paginate :page => params[:page],
        :per_page => $perPage,
        :order => "orders asc"
    end
    #查询项参数
    @ad = Ad.new
    @ad.name = name
    #功能名
    @titles = "广告列表"
  end

  def edit
    @ad = Ad.find(params[:id])
    @titles = "编辑广告---"+@ad.name
  end

  def update
    @ad = params[:ad]
    if Ad.update(@ad[:id], @ad)
      flash[:notice] = '更新成功'
      redirect_to :action=>'index'
    else
      render :action => "edit"
    end
  end

  def show
    @ad = Ad.find(params[:id])
    @titles = "广告信息---"+@ad.name
  end

  def delete
    ad = Ad.find(params[:id])
    ad.destroy
    redirect_to :action=>'index'
  end

end
